<?php

/**
 * Sign in user session
 * 
 * @project : Loobica Ads
 * @author  : Dino
 * @date    : 13 Jan 2014
 * 
 */
if (!defined('BASEPATH'))
  exit('No direct script access allowed');

class Signin extends LB_Controller {

  public function __construct() {
    parent::__construct();
    $this->prevent_signed();
  }

  public function index() {

    $user_email = null;
    $user_password = null;

    // Processing user captcha security always for signup
    $user_security = $this->user_security->run();

    // If post request
    if ($this->is_post()) {

      // Validate form
      $this->form_validation->set_rules('user_email', 'Email', 'required|valid_email|min_length[10]|max_length[50]|valid_registerd_email');
      $this->form_validation->set_rules('user_password', 'password', 'required');
      $this->form_validation->set_rules('user_security_answer', 'security answer', 'valid_security_answer');

      // Form success
      if ($this->form_validation->run()) {

        // Getting inputs
        $user_email = trim($this->input->post('user_email', TRUE));
        $user_password = $this->input->post('user_password', TRUE);

        // Signing in user
        $sign_in = $this->user_session->sign_in($user_email, $user_password);

        // If successfully signed in
        if ($sign_in) {

          // Clear captcha if successfully signed in
          $this->user_security->clear();

          // Redirecting to proper page
          $this->redirect($this->redirect_to);
        }
      }
    }

    // Preparing and loading view
    $view_data = array();
    $view_data['title'] = lang('sign_in');
    $view_data['user_email'] = $user_email;
    $view_data['user_security'] = $user_security;

    // Loading view
    $this->load_view('signin/signin', $view_data);
  }

}
